# -*- encoding: utf-8 -*-
from django.template import Library
from wagglyblog.models import *


register = Library()

@register.inclusion_tag('wagglyblog/templatetags/archive_list.html')
def archive_list():
    """
    Lists all months with blog posts in it starting with the latest

    Template: ``wagglyblog/templatetags/archive_list.html``

    Context::

        object_list

    """
    try:
        dates = Post.published.dates('pub_date','month','DESC')
        return {'object_list': dates }
    except:
        return None

@register.inclusion_tag('wagglyblog/templatetags/archive_latest.html')
def archive_latest():
    """
    List of the last 10 blog posts

    Template: ``wagglyblog/templatetags/archive_latest.html``

    Context::

        object_list
    """
    try:
        posts = Post.published.order_by('-pub_date')[:10]
        return {'object_list': posts }
    except:
        return None

@register.inclusion_tag('wagglyblog/templatetags/post_categorie_list.html')
def post_category_list(postid):
    """
    List of this postid's categories

    Usage::

        {% post_category_list post.id %}

    Template: ``wagglyblog/templatetags/post_categorie_list.html``

    Context::

        category_list
    """
    try:
        p = Post.objects.get(pk=postid)
        cats = p.categories.all()
        return {'category_list': cats}
    except:
        return None

@register.inclusion_tag('wagglyblog/templatetags/categorie_listbox.html')
def category_listbox():
    """
    List of all categories

    Template: ``wagglyblog/templatetags/categorie_listbox.html``

    Context::

        category_list
    """
    try:
        cats = Category.objects.all()
        return {'category_list': cats}
    except:
        return None
